/**
 * User 业务逻辑实现类
 * @author gaocc
 * @date 2014-08-25
 */
package com.chnbst.bstyun.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.chnbst.bstyun.core.base.BaseService;
import com.chnbst.bstyun.core.common.helper.DaoHelper;
import com.chnbst.bstyun.model.pojo.Company;
import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.model.vo.UserVo;
import com.chnbst.bstyun.service.UserService;

@Transactional
@Service
public class UserServiceImpl  extends BaseService implements UserService{
	
	//声明命名空间
	private final static String nameSpace = "com.chnbst.bstyun.mapper.UserMapper";
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public boolean insertUser(User user) {
		return DaoHelper.insert(nameSpace, "insertUser", user);
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public List<User> emailExists(String userEmail) {
		return (List<User>)DaoHelper.list(nameSpace, "selectUserByEmail",userEmail);
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public List<User> TelExists(String tel) {
		return (List<User>)DaoHelper.list(nameSpace, "selectUserByPhone", tel);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public User loginByEmail(User user) {
		User loginUser = (User)DaoHelper.query(nameSpace, "selectUserByEmail", user);
		return loginUser;
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public User loginByTel(User user) {
		User loginUser = (User)DaoHelper.query(nameSpace, "selectUserByTel", user);
		return loginUser;
	}
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public boolean updateUserByAccount(User user) {
		return DaoHelper.update(nameSpace, "updatePwdByAccount", user);
	}
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public boolean updateUserLastTime(User user) {
		return DaoHelper.update(nameSpace, "updateByPrimaryKeySelective", user);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public boolean findPassword(User user) {
		return DaoHelper.update(nameSpace, "findPassword", user);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public Object boundEmailExists(String boundEmail) {
		return DaoHelper.query(nameSpace, "selectUserByBoundEmail", boundEmail);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public Object boundTelExists(String boundTel) {
		return DaoHelper.query(nameSpace, "selectUserByBoundTel", boundTel);
	}
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public boolean updateUserByKey(User user) {
		return DaoHelper.update(nameSpace, "updateByPrimaryKeySelective", user);
	}
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public User selectUserInfoByUserId(Integer userId) {
		return (User)DaoHelper.query(nameSpace, "selectByPrimaryKey", userId);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public boolean updateUserSafeInfo(User user) {
		return DaoHelper.update(nameSpace, "updateBoundSafeInfo", user);
	}
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public Object selectUserPsdByBoundEmail(String boundEmail) {
		return DaoHelper.query(nameSpace, "selectUserPsdByBoundEmail", boundEmail);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public Object selectUserPsdByBoundTel(String boundTel) {
		return DaoHelper.query(nameSpace, "selectUserPsdByBoundTel", boundTel);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public Object selectUserByAccount(User user) {
		return DaoHelper.query(nameSpace, "selectUserByAccount", user);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public boolean insertCompany(Company company) {
		boolean success = false;
		User user = new User();
		try {
			DaoHelper.insert("com.chnbst.bstyun.mapper.CompanyMapper", "insertSelective", company);
			user.setCompanyId(company.getCompanyId());
			DaoHelper.update(nameSpace, "updateByPrimaryKeySelective", user);
			success = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return success;
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public User selectUserByConditons(Map<String, Object> map) {
		return (User)DaoHelper.query(nameSpace, "selectUserByConditons", map);
	}

	@Override
	public boolean updateUserByUserId(User user) {
		return DaoHelper.update(nameSpace, "updateByUserId", user);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public UserVo selectUserVoByUserId(Integer userId) {
		return (UserVo)DaoHelper.query(nameSpace, "selectUserVoByUserId", userId);
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public List<User> selectUserByBelongCompanyName(String belongCompanyName) {
		return (List<User>)DaoHelper.list(nameSpace, "selectUserByBelongCompanyName", belongCompanyName);
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public List<User> selectUsers(Map<String, Object> map) {
		return (List<User>)DaoHelper.list(nameSpace, "selectUsers", map);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public int selectUserCount(Map<String, Object> map) {
		return DaoHelper.count(nameSpace, "selectUserCount", map);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public boolean deleteUser(User user) {
		return DaoHelper.delete(nameSpace, "deleteUser", user);
	}
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public boolean updateAccountMoneyByUserId(User user) {
		return DaoHelper.update(nameSpace, "updateAccountMoneyByUserId", user);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public int selectUserCountByIsYun(Map<String, Object> map) {
		return DaoHelper.count(nameSpace, "selectUserCountByIsYun", map);
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public List<UserVo> selectUsersByIsYun(Map<String, Object> map) {
		return (List<UserVo>)DaoHelper.list(nameSpace, "selectUsersByIsYun", map);
	}

	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public int selectCompanyEmpCountByDomain(String userEmail) {
		return DaoHelper.count(nameSpace, "selectCompanyEmpCountByDomain", userEmail);
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public List<UserVo> selectCompanyEmpListByDomain(Map<String, Object> map) {
		return (List<UserVo>)DaoHelper.list(nameSpace, "selectCompanyEmpListByDomain",map);
	}
	
    @Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public Double selectAccountMoney(Map<String, Object> map) {
		return (Double)DaoHelper.query(nameSpace, "selectAccountMoney", map);
	}

    @SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public List<UserVo> selectUserVoList(Map<String, Object> map) {
		return (List<UserVo>)DaoHelper.list(nameSpace, "selectUserVoList",map);
	}

    @Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public int selectUserCountByRoleId(String roleId) {
		return DaoHelper.count(nameSpace, "selectUserCountByRoleId", roleId);
	}

    @Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@SuppressWarnings("unchecked")
	@Override
	public List<UserVo> selectUsersByRoleId(Map<String, Object> map) {
		return (List<UserVo>) DaoHelper.list(nameSpace, "selectUsersByRoleId", map);
	}

    @Transactional(propagation=Propagation.REQUIRED,readOnly=true)
   	@SuppressWarnings("unchecked")
	@Override
	public List<User> selectByRoleId(Map<String, Object> map) {
		return (List<User>) DaoHelper.list(nameSpace, "selectByRoleId", map);
	}

    @Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public boolean updateUser(User user) {
		return DaoHelper.update(nameSpace, "updateUser", user);
	}

    @Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public int selectUserCountByCondition(Map<String, Object> map) {
		return DaoHelper.count(nameSpace, "selectUserCountByCondition", map);
	}

	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@Override
	public List<UserVo> selectUserVoByCondition(Map<String, Object> map) {
		return (List<UserVo>) DaoHelper.list(nameSpace, "selectUserVoByCondition", map);
	}
}
